<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\developer;

use addons\cloud_market\controller\api\CloudMarketApiController;
use addons\cloud_market\model\CloudMarketDeveloper;
use think\facade\Filesystem;

class Encryption extends CloudMarketApiController
{
    protected $notApiCheck = ['*'];

    public function initialize()
    {
        parent::initialize();
    }


    public function getUserEncryption()
    {
        $user = $this->getUser();
        return $this->success('获取成功', $user['developer']);
    }

    // 执行加密
    public function execute()
    {
        $file = request()->file('file');
        // 获取文件属性
        $api = "http://swoole.dkyx.cc/encrypt";
        // TODO 这里需要调整为上传到对象存储
        $savename = Filesystem::putFile('uploads/swoole', $file);
        $data = [
            'zip_url'       => $this->request->domain() . '/' . $savename,
            'php_version'   => input('php_version'),
            'save_comments' => input('save_comments'),
            'zip_type'      => 'url'
        ];
        $res = httpPost($api, $data,true);
        $res = json_decode($res, true);
        if ($res['code'] == 0) {
            $user = $this->getUser();
            $dev = (new CloudMarketDeveloper())->where(['user_id'=>$user['id']])->find();
            $dev->sc_size = $dev->sc_size - intval($file->getSize() / 1024);
            $dev->sc_number = $dev->sc_number + 1;
            $dev->sc_size_all = $dev->sc_size_all + intval($file->getSize() / 1024);
            $dev->save();

            $down_url = "http://swoole.dkyx.cc/tmp_down?down_file=" . $res['data']['path'];
            // 删除文件
            Filesystem::delete($savename);
            return $this->success("加密成功", ['down_url' => $down_url]);
        } else {
           return $this->error("加密失败");
        }
    }
}
